package com.hccl.service.classifier;

import com.hccl.service.classifier.runSession;
import com.hccl.service.classifier.runRnnSession;
import org.ansj.splitWord.analysis.ToAnalysis;

import java.io.FileInputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static com.hccl.service.classifier.dataprovider.readDic;
import static com.hccl.service.classifier.dataprovider.readMap;
import static com.hccl.config.Constants.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;


/**
 * Created by yang on 2018/8/3.
 */
public class tfPredict {
    BlockingQueue<ToAnalysis> segs = new LinkedBlockingQueue<>();
    Map<String ,Integer> word2IdMap = new HashMap<>();
    Map<String ,Integer> pos2IdMap = new HashMap<>();
    Map<String ,String> Id2actionMap = new HashMap<>();
    Map<String ,String> Id2targetMap = new HashMap<>();
    Map<String ,String> Id2typeMap = new HashMap<>();
    Map<String ,Integer> commandTarget2IdMap = new HashMap<>();
    public static int threadNum = 2;
    Map<String,runRnnSession> sessionMap = new HashMap<>();
    private static final tfPredict predict = new tfPredict();
    private tfPredict()  {

        try {

            //logger.info("开始new tfPredict");
            //System.out.print("开始new tfPredict");
            word2IdMap = readDic(wordDicurl);
            pos2IdMap = readDic(posDicurl);
            Id2actionMap = readMap(actionDicurl);
            Id2targetMap = readMap(targetDicurl);
            Id2typeMap = readMap(typeDicurl);
            //char2IdMap = readDic(charDicurl);
            commandTarget2IdMap = readDic(commandTargetDicurl);

            for (int i=0;i<threadNum;i++){
                ToAnalysis toAnalysis = new ToAnalysis();
                toAnalysis.parseStr(" ");
                segs.add(toAnalysis);
            }
            Path rnnModelPath = Paths.get(libraryVersion, "rnnmodel.pb");
            runRnnSession rnn_session = new runRnnSession(rnnModelPath);
            sessionMap.put("rnn",rnn_session);
            //System.out.print("结束new tfPredict");

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static tfPredict getInstance(){
        return predict;
    }

    @SuppressWarnings("unchecked")
    public ClassifierResult predict(String inputString) throws Exception {

        ClassifierResult result;
        result = new ClassifierResult();
        ArrayList<List> tmpResults = new ArrayList<>();
        List<ClassifierResult> res = new ArrayList<>();

        ToAnalysis toAnalysis = segs.take();

        //logger.info(Integer.toString(segs.size()));
        dataprovider dataProvider = new dataprovider(word2IdMap, pos2IdMap, toAnalysis, inputString);

        for (Map.Entry<String,runRnnSession> entry : sessionMap.entrySet()) {

            Map featureMap = new HashMap();
            featureMap = dataProvider.getData(true);
            /*
            List input = new ArrayList<>();
            input.add(1);
            input.add(2);

            featureMap.put("data",input);*/
            /*
            if(entry.getKey().equals("dcnn")){
                featureMap = dataProvider.getData(false);

            }else {
                featureMap = dataProvider.getData(true);    //att 模型的补零
            }*/

            res = entry.getValue().run(featureMap);
            //res.put("model",entry.getKey());
            //tmpResults.add(res);
        }
        //合并
        result = res.get(0);
        result.setAction(Id2actionMap.get(result.getAction()));
        result.setTarget(Id2targetMap.get(result.getTarget()));
//        result.setType(Id2typeMap.get(result.getType()));
        //result.setType(res.get(0).getType().toString());
        if (commandTarget2IdMap.containsKey(result.getTarget())){
            result.setType("命令");
        }else {
            result.setType("业务");
        }
        segs.add(toAnalysis);
        return result;

    }
}
